package edu.hawaii.duedates;

import java.util.Date;

/**
 * Book class that extends from a generic Item class.
 * @author Anthony M. Du
 *
 */
public class Book implements Comparable<Book> {
  // Book title.
  private final String title;
  // Due date
  private final Date duedate;
  
  /**
   * Book constructor with due date.
   * @param title of book.
   * @param duedate of book.
   */
  public Book(String title, Date duedate) {
    this.title = title;
    this.duedate = new Date(duedate.getTime());
  }

  /**
   * CompareTo method uses the book's due date attribute 
   * to compare itself with another book object.
   * @param o is a Book instance.
   * @return int of 0, 1 or -1.
   */
  public int compareTo(Book o) {
    return this.duedate.compareTo(o.getDuedate());
  }
  
  /**
   * Equals is used to check equality of a give book object.
   * @param obj The object being passed for equality check.
   * @return boolean value of true/false.
   */
  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Book) {
      Book b = (Book) obj;
      return b.getTitle().equals (this.title) && b.getDuedate().equals(this.duedate);
    }
    return false;
  }
  
  /**
   * HashCode for generating a unique hashcode coupled by the book's attributes.
   * @return int a unique book hashcode.
   */
  @Override 
  public int hashCode() {
    return this.duedate.hashCode() + this.title.hashCode();
  }
  
  /**
   * GetTitle gets the book's title.
   * @return String of the book's title.
   */
  public String getTitle() {
    return title;
  }

  /**
   * GetDuedate gets the book's due date.
   * @return String of the book's due date.
   */
  public Date getDuedate() {
    return new Date(duedate.getTime());
  }
  
  /**
   * ToString displays the book information along with its due date status.
   * @return String about book.
   */
  public String toString() {
    return "Info: " + this.title + "\nStatus: " + this.duedate;
  }
}
